<template>
  <!-- 给这个div定义一个dom节点wrapper -->
  <div class='list' ref="wrapper">
    <div>
      <div class='area'>
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class='area'>
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper"
            v-for="item of hotCities"
            :key="item.id"
            @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <!-- 这里的dom节点是动态的，因此需要:ref绑定，否则只能拿到cityGroupKey的原始值，而不是变量值 -->
      <div class='area'
        v-for="(cityInfo, cityGroupKey) of cities"
        :key="cityGroupKey"
        :ref="cityGroupKey"
      >
        <div class="title border-topbottom">{{cityGroupKey}}</div>
        <div class="item-list">
          <div class="item border-bottom"
            v-for="city of cityInfo"
            :key="city.id"
            @click="handleCityClick(city.name)">{{city.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    letter: String,
    hotCities: Array,
    cities: Object
  },
  methods: {
    handleCityClick (city) {
      // 派发一个changeCity的Action
      // this.$store.dispatch('changeCity', city)
      // 也可以省略action,直接调用mutations
      // commit 这行，可以用...mapMutations(['mutChangeCity'])替代
      // this.$store.commit('mutChangeCity', city)
      this.mutChangeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['mutChangeCity'])
  },
  computed: {
    // 另外一种写法。参考Header.vue, ...是展开符
    ...mapState({
      currentCity: 'city'
    })
  },
  data () {
    return {
      scroll: ''
    }
  },
  mounted () {
    // better-scroll 插件会导致click事件失效，这个配置项是解决这个问题的
    const options = {
      click: true,
      tap: true
    }
    // 获取wrapper的dom节点
    this.scroll = new Bscroll(this.$refs.wrapper, options)
  },
  watch: {
    // 监听letter这个变量,这个名字要和props里的要一致
    letter () {
      if (this.letter) {
        const ele = this.$refs[this.letter][0]
        this.scroll.scrollToElement(ele)
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    // 防止页面溢出而产生滚动条
    overflow: hidden
    // 绝对定位及定义高度，目的是固定城市输入框，不产生滚动条
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    // 标题栏样式
    .title
      line-height: .54rem
      background: #eee
      padding-left: .2rem
      color: #666
      font-size: .26rem
    // 内容栏样式
    .button-list
      // 由于父级元素没有高度，下面的元素会顶上去，造成页面的塌陷, 父级加个overflow:hidden属性，这样父级的高度就随子级容器及子级内容的高度而自适应
      overflow: hidden
      // 预留右边空间
      padding: .1rem .6rem .1rem .1rem
      // 该div下的内容，向左浮动，并指定宽度
      .button-wrapper
        float: left
        width: 33.33%
        // 被button-wrapper样式包裹，定义内容细节样式
        .button
          margin: .1rem
          padding: .1rem 0
          text-align: center
          border: .02rem solid #ccc
          border-radius: .06rem
    .item-list
      .item
        line-height: .76rem
        padding-left: .2rem
</style>
